const { writeFileSync } = require('fs');
const path = require('path');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
module.exports = (app, config) => {
  const arr = require('../router');
  let obj = {};
  arr.forEach(item => {
    let { path, method, doc = {} } = item;
    let { tags, summary, description, body, query = {}, responses } = doc;
    if (!obj[path]) obj[path] = {};
    let params = Object.keys(query).map(e => {
      return {
        in: "query",
        name: e,
        description: e.description || '',
        type: e.type || query[e],
        ...query[e]
      }
    })
    obj[path][method] = {
      tags,
      summary,
      description,
      parameters: [
        {
          "in": "body",
          "name": "body",
          "description": "body",
          "required": true,
          "schema": {
            "properties": body
          }
        },
        ...params,
      ],
      responses: responses
    }
  });
  const swaggerJson = require('./swagger.json');
  swaggerJson.paths = obj;
  swagger(app, swaggerJson);
}
function swagger(app, swaggerJson) {
  app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerJson));
};


// "/pet": {
//   "post": {
//     "tags": [  "pet" ],
//     "summary": "接口名称",
//     "description": "接口描述",
//     "parameters": [
//       {
//         "in": "body",
//         "name": "body",
//         "description": "Pet object that needs to be added to the store",
//         "required": true,
//         "schema": {}
//       },
// {
//         "in": "status",
//         "name": "query",
//         "description": "Pet object that needs to be added to the store",
//         "required": true, 
//       }
//     ],
//     "responses": {}
//   }
// }

// tags: ['user'],
// name: 'name',
// description: '描述',
// body: {},
// query: {},
// response: {}



